package pacmanreturns;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Game extends JApplet implements Runnable {
	public static final int STOP = 0;
	public static final int UP = 1;
	public static final int LEFT = 2;
	public static final int DOWN = 3;
	public static final int RIGHT = 4;
	public static final int WIDTH = 560;
	public static final int HEIGHT = 640;

	public static boolean isRunning = true;
	public AudioManager audio;

	private int frame;
	private int delay = 10;
	private Thread animator;

	private Image buffer;
	private Graphics2D gBuffer;
	private Maze maze;
	private Pacman pacman;
        private Ghost pinky;
        private Ghost blinky;
        private Ghost inky;
	
	public void init() {
		this.addKeyListener(new KeyHandler());

		audio = new AudioManager(this);
		audio.play(0);

		maze = new Maze(this, audio);
		pacman = new Pacman(this, maze);
                pinky = new Ghost(this, maze);
                inky = new Ghost(this, maze);
                blinky = new Ghost(this, maze);
		maze.setStart(pacman);
                maze.setStart(pinky);
                maze.setStart(inky);
                maze.setStart(blinky);
	}

	public void start() {
		animator = new Thread(this);
		animator.start();
	}

    public void run() {
		long time = System.currentTimeMillis();
		while (Thread.currentThread() == animator) {
		    repaint();

		    try {
				time += delay;
				Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
		    } catch (InterruptedException e) {
				break;
		    }
		    frame++;
		}
	}

	public void stop() {
		animator = null;
    }

	public void update(Graphics g) {
		if (buffer == null) {
			buffer = createImage(Game.WIDTH, Game.HEIGHT);
			gBuffer = (Graphics2D)buffer.getGraphics();
			gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
								RenderingHints.VALUE_ANTIALIAS_ON);
		}

		gBuffer.setColor(Color.BLACK);
		gBuffer.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);

		maze.draw();
		pacman.draw();
                pinky.draw();
                blinky.draw();
                inky.draw();
		g.drawImage(buffer, 0, 0, this);
	}

	public void paint(Graphics g) {
		update(g);
	}

	public void arc(int x, int y, int width,
					 int height, int startAngle, int arcAngle) {
		gBuffer.fillArc(x, y, width, height, startAngle, arcAngle);
	}

	public void rectangle(int x, int y, int width, int height) {
		gBuffer.fillRect(x, y, width, height);
	}

	public void setColor(Color inColor) {
		gBuffer.setColor(inColor);
	}

	public void text(String text, int x, int y) {
		gBuffer.drawString(text, x, y);
	}

	public void move(int inDirection) {
		pacman.setDirection(inDirection);
	}

	class KeyHandler implements KeyListener {
		public void keyTyped(KeyEvent e) {
		}

		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
				case KeyEvent.VK_UP:
					move(Game.UP);
					break;
				case KeyEvent.VK_DOWN:
					move(Game.DOWN);
					break;
				case KeyEvent.VK_RIGHT:
					move(Game.RIGHT);
					break;
				case KeyEvent.VK_LEFT:
					move(Game.LEFT);
					break;
			}
		}

		public void keyReleased(KeyEvent e) {
		}
	}
}